<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品id" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          clearable
          placeholder="请输入订单状态">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数量" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总价" prop="total">
        <el-input
          v-model="queryParams.total"
          placeholder="请输入总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="true"
          @click="handleAdd"
          v-hasPermi="['backstage:orderDetail:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="true"
          @click="handleUpdate"
          v-hasPermi="['backstage:orderDetail:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="true"
          @click="handleDelete"
          v-hasPermi="['backstage:orderDetail:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['backstage:orderDetail:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单细节id" align="center" prop="id"/>
      <el-table-column label="订单id" align="center" prop="orderId"/>
      <el-table-column label="商品id" align="center" prop="goodsId"/>
      <el-table-column label="商品名称" align="center" prop="goods.name">
      </el-table-column>
      <el-table-column label="数量" align="center" prop="num"/>
      <el-table-column label="总价" align="center" prop="total">
        <template slot-scope="scope">
          {{ scope.row.total }} 元
        </template>
      </el-table-column>
      <el-table-column label="收货地址" align="center" prop="address">
        <template slot-scope="scope">
          <el-tooltip placement="top">
            <div slot="content">
              收货人：{{ scope.row.address.name }}<br/>
              电&emsp;话：{{ scope.row.address.phone }}<br>
              省市区：{{ scope.row.address.province }}-{{ scope.row.address.city }}-{{ scope.row.address.area }}<br>
              地&emsp;址：{{ scope.row.address.address }}
            </div>
            <el-button>查看地址</el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="细节状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.status == 0">未发货</el-tag>
          <el-tag type="success" v-if="scope.row.status == 1">已发货</el-tag>
          <el-tag v-if="scope.row.status == 2">已收货</el-tag>
          <el-tag type="info" v-if="scope.row.status == 3">已评价</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="order.status">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.order.status == 0">未付款</el-tag>
          <el-tag v-if="scope.row.order.status == 1">已付款</el-tag>
          <el-tag type="success" v-if="scope.row.order.status == 2">交易完成</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.order.status == 1 && scope.row.status == 0"
                     @click="handleAlter(scope.row.id)">发货
          </el-button>

          <el-button type="primary" :disabled="multiple" v-if="scope.row.order.status != 1 || scope.row.status != 0">
            发货
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单细节管理对话框 -->
    <!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
    <!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
    <!--      </el-form>-->
    <!--      <div slot="footer" class="dialog-footer">-->
    <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
    <!--        <el-button @click="cancel">取 消</el-button>-->
    <!--      </div>-->
    <!--    </el-dialog>-->
  </div>
</template>

<script>
import {
  listOrderDetail,
  getOrderDetail,
  delOrderDetail,
  addOrderDetail,
  updateOrderDetail,
  alterStatus
} from "@/api/backstage/orderDetail";
import Cookies from "js-cookie";

import {getShopByUid} from '@/api/backstage/shopinfo';

export default {
  name: "OrderDetail",
  data() {
    return {
      myUID: '',
      myDept: '',
      myShop: {},


      options: [{
        value: '0',
        label: '未发货'
      }, {
        value: '1',
        label: '已发货'
      }, {
        value: '2',
        label: '已收货'
      }, {
        value: '3',
        label: '已评论'
      }],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单细节管理表格数据
      orderDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        orderId: null,
        status: null,
        num: null,
        total: null,
        shopId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsId: [
          {required: true, message: "商品id不能为空", trigger: "blur"}
        ],
        orderId: [
          {required: true, message: "订单id不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.myDept = Cookies.get("myDeptName");
    this.myUID = Cookies.get("myUID");
    if (this.myDept == "店铺") {
      getShopByUid(this.myUID).then(res => {
        this.myShop = res.data;
        this.queryParams.shopId = this.myShop.id;
        console.log(res);
      });
    }


    var that = this;
    setTimeout(function () {
      that.getList();
    }, 500);
  },
  methods: {
    /** 查询订单细节管理列表 */
    getList() {
      this.loading = true;
      listOrderDetail(this.queryParams).then(response => {
        this.orderDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        orderId: null,
        status: null,
        num: null,
        total: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单细节管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单细节管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrderDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单细节管理编号为"' + ids + '"的数据项？').then(function () {
        return delOrderDetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('backstage/orderDetail/export', {
        ...this.queryParams
      }, `orderDetail_${new Date().getTime()}.xlsx`)
    },

    /** 店家发货操作 */
    handleAlter(id) {
      alterStatus(id).then(() => {
        this.$modal.msgSuccess("发货成功");
        this.getList();
      });
    }
  }
};
</script>
